<?php

namespace Sistema\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Sistema\AdminBundle\Entity\Club
 *
 * @ORM\Table(name="Club")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Club
{
    /**
     * @Assert\Image(maxSize = "500k")
     */
    public $file;

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $nombre
     *
     * @ORM\Column(name="nombre", type="string", length=200, nullable=false)
     */
    private $nombre;

    /**
     * @var string $logo
     *
     * @ORM\Column(name="logo", type="string", length=200, nullable=true)
     */
    private $logo;
    
    /**
     * @var datetime $created
     *
     * @ORM\Column(name="created", type="datetime")
     */
    private $created;
    
//    /**
//     * @var Equipo
//     * 
//     * @ORM\OneToMany(targetEntity="Club", mappedBy="id_club", cascade={"remove"})
//     */
//    private $idEquipo;

    public function __toString() {
        return $this->getNombre();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nombre
     *
     * @param string $nombre
     * @return Club
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;
    
        return $this;
    }

    /**
     * Get nombre
     *
     * @return string 
     */
    public function getNombre()
    {
        return $this->nombre;
    }

    /**
     * Set logo
     *
     * @param string $logo
     * @return Club
     */
    public function setLogo($logo)
    {
        $this->logo = $logo;
    
        return $this;
    }

    /**
     * Get logo
     *
     * @return string 
     */
    public function getLogo()
    {
        return $this->logo;
    }
    
    /**
     * Set created
     *
     * @param datetime $created
     */
    public function setCreated($created)
    {
        $this->created = $created;
    }

    /**
     * Get created
     *
     * @return datetime 
     */
    public function getCreated()
    {
        return $this->created;
    }
    
    public function getAbsolutePath()
    {
        return null === $this->logo ? null : $this->getUploadRootDir().'/'.$this->logo;
    }

    public function getWebPath()
    {
        return null === $this->logo ? null : $this->getUploadDir().'/'.$this->logo;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory logo where uploaded documents should be saved
        return __DIR__.'/../../../../'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        return 'web/uploads/club/';
    }
    
    /**
     * @ORM\PostLoad
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function preUpload()
    {
        $this->created =  new \DateTime();
        // the file property can be empty if the field is not required
        if (null === $this->file) {
            return;
        }

        // we use the original file name here but you should
        // sanitize it at least to avoid any security issues

        // set the logo property to the filename where you'ved saved the file
        if (null === $this->logo) {
            $this->setLogo(uniqid().'.'.$this->file->guessExtension());
        }else{
            $this->removeUpload();
//            $extension = pathinfo($this->logo, PATHINFO_EXTENSION);
//            $nombre_base = basename($this->logo, '.'.$extension);
            $this->setLogo(uniqid().'.'.$this->file->guessExtension());
        }
    }

    /**
     * @ORM\PostPersist
     * @ORM\PostUpdate
     */    
    public function upload()
    {
        // the file property can be empty if the field is not required
        if (null === $this->file) {
            return;
        }

        // move takes the target directory and then the target filename to move to
        $this->file->move($this->getUploadRootDir(), $this->logo);

        unset($this->file);
    }
    
    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }
}
